{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "e3fae053-c02c-4c9c-8b02-e896893fa77c",
   "metadata": {},
   "outputs": [],
   "source": [
    "from tqdm import tqdm\n",
    "import multiprocessing\n",
    "import numpy as np\n",
    "import sys\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "76549094-c553-4fb1-b522-983279a72e96",
   "metadata": {},
   "source": [
    "## 数据预处理"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9d5fb2c2-8a73-447e-b324-8761950f8404",
   "metadata": {},
   "source": [
    "### 测试集、训练集划分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "973e98ac-25f6-48e0-9bab-fa502e49dadc",
   "metadata": {},
   "outputs": [],
   "source": [
    "labels =        [ 'down',   'go',   'left',     'no',   'off',  'on',   'right',    'stop',     'up',   'yes']\n",
    "labels_num =    [   0,      1,      2,          3,      4,      5,      6,          7,          8,      9 ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "cc555983-1a03-4347-a67d-8ffdcde3190a",
   "metadata": {},
   "outputs": [],
   "source": [
    "data_dir = '../../dataset/Data_useful/'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "e32bbb6b-836a-4668-9de9-7db6faf51db2",
   "metadata": {},
   "outputs": [],
   "source": [
    "def data_split(file_name):\n",
    "    ratio = 0.7\n",
    "    datas = np.load(file_name)\n",
    "    total_num = np.shape(datas)[0]\n",
    "    train_num = round(ratio*total_num)\n",
    "    test_num = total_num - train_num\n",
    "    \n",
    "    train_data = datas[:train_num]\n",
    "    test_data = datas[-test_num:]\n",
    "    \n",
    "    return train_data,test_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "704bc921-59bb-4cae-957e-c40c0de08398",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 10/10 [00:00<00:00, 120.37it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Processing lable: down\n",
      "Processing lable: go\n",
      "Processing lable: left\n",
      "Processing lable: no\n",
      "Processing lable: off\n",
      "Processing lable: on\n",
      "Processing lable: right\n",
      "Processing lable: stop\n",
      "Processing lable: up\n",
      "Processing lable: yes\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "train_set = []\n",
    "test_set = []\n",
    "for lab in tqdm(labels):\n",
    "    print('Processing lable: {}'.format(lab))\n",
    "    file_name = data_dir + \"/\" + lab + '.npy'\n",
    "    train_data,test_data = data_split(file_name)\n",
    "    train_set.append(train_data)\n",
    "    test_set.append(test_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "a6150a25-745b-429d-88e5-927adbb44a29",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<ipython-input-29-a6ac225dc432>:1: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.\n",
      "  train_set = np.array(train_set)\n",
      "<ipython-input-29-a6ac225dc432>:2: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.\n",
      "  test_set = np.array(test_set)\n"
     ]
    }
   ],
   "source": [
    "train_set = np.array(train_set)\n",
    "test_set = np.array(test_set)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "55795004-6680-46a0-bed5-d795f651c154",
   "metadata": {},
   "outputs": [],
   "source": [
    "train_file =  data_dir + \"/\"  + 'train.npy'\n",
    "test_file = data_dir + \"/\"  + 'test.npy'\n",
    "np.save(train_file, train_set)\n",
    "np.save(test_file, test_set)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c474d29d-15b8-47fc-a966-7de57bdbd84d",
   "metadata": {},
   "source": [
    "## 进行训练"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "022edf18-b29c-486c-99d9-a62d5b17d83b",
   "metadata": {},
   "source": [
    "### 读取数据&前处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e66caef2-f2be-4bab-b710-d12b1de5beb1",
   "metadata": {},
   "outputs": [],
   "source": [
    "data_dir = '../../dataset/Data_useful/'\n",
    "train_file =  data_dir + \"/\"  + 'train.npy'\n",
    "test_file = data_dir + \"/\"  + 'test.npy'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d700f733-9648-4f88-8061-0bb11d4a6e31",
   "metadata": {},
   "outputs": [],
   "source": [
    "train_set = np.load(train_file, allow_pickle=True)\n",
    "test_set = np.load(test_file, allow_pickle=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "77321888-0ed3-4311-8d57-376e65516f70",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(26284, 51, 15)\n"
     ]
    }
   ],
   "source": [
    "train_data = train_set[0]\n",
    "for i in train_set[1:]:\n",
    "    train_data = np.append(train_data,i, axis=0)\n",
    "print(train_data.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "f7b67e36-5291-497b-a572-af28ff86e51a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1340484, 15)\n"
     ]
    }
   ],
   "source": [
    "the_shape = train_data.shape\n",
    "train_items = train_data.reshape(the_shape[0]*the_shape[1],the_shape[2])\n",
    "print(train_items.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "c67db5bb-5677-4b14-85dc-3e1bc43e6415",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(11262, 51, 15)\n"
     ]
    }
   ],
   "source": [
    "test_data = test_set[0]\n",
    "for i in test_set[1:]:\n",
    "    test_data = np.append(test_data,i, axis=0)\n",
    "print(test_data.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "d5aff241-23da-47ed-9161-41df8b52cbd2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(574362, 15)\n"
     ]
    }
   ],
   "source": [
    "the_shape = test_data.shape\n",
    "test_items = test_data.reshape(the_shape[0]*the_shape[1],the_shape[2])\n",
    "print(test_items.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e3155e10-dcb6-4a86-89bf-01967a945b8c",
   "metadata": {},
   "outputs": [],
   "source": [
    "test_x = test_items[:,:-1]\n",
    "test_y = test_items[:,-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5f0c5f6d-aa76-4dce-ace6-1621238e0fdc",
   "metadata": {},
   "outputs": [],
   "source": [
    "array_nan = np.array([np.nan]*np.shape(test_x)[0])\n",
    "test_x = np.column_stack((test_x, array_nan))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8681fa85-4385-4af0-b74c-8ce19dedb4ec",
   "metadata": {},
   "outputs": [],
   "source": [
    "test_x = test_x[0:5]\n",
    "print(test_x.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a582b5a8-e36f-4919-9534-647e07af9bce",
   "metadata": {},
   "source": [
    "### Spflow训练模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "a1fa3a62-6825-43df-869a-c35899134e59",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Process ForkPoolWorker-305:\n",
      "Process ForkPoolWorker-298:\n",
      "Process ForkPoolWorker-213:\n",
      "Process ForkPoolWorker-236:\n",
      "Process ForkPoolWorker-239:\n",
      "Process ForkPoolWorker-251:\n",
      "Process ForkPoolWorker-306:\n",
      "Process ForkPoolWorker-226:\n",
      "Process ForkPoolWorker-250:\n",
      "Process ForkPoolWorker-254:\n",
      "Process ForkPoolWorker-252:\n",
      "Process ForkPoolWorker-242:\n",
      "Process ForkPoolWorker-240:\n",
      "Process ForkPoolWorker-218:\n",
      "Process ForkPoolWorker-202:\n",
      "Process ForkPoolWorker-235:\n",
      "Process ForkPoolWorker-214:\n",
      "Process ForkPoolWorker-310:\n",
      "Process ForkPoolWorker-303:\n",
      "Process ForkPoolWorker-249:\n",
      "Process ForkPoolWorker-195:\n",
      "Process ForkPoolWorker-237:\n",
      "Process ForkPoolWorker-215:\n",
      "Process ForkPoolWorker-307:\n",
      "Process ForkPoolWorker-278:\n",
      "Process ForkPoolWorker-204:\n",
      "Process ForkPoolWorker-234:\n",
      "Process ForkPoolWorker-231:\n",
      "Process ForkPoolWorker-241:\n",
      "Process ForkPoolWorker-245:\n",
      "Process ForkPoolWorker-211:\n",
      "Process ForkPoolWorker-276:\n",
      "Process ForkPoolWorker-220:\n",
      "Process ForkPoolWorker-227:\n",
      "Process ForkPoolWorker-266:\n",
      "Process ForkPoolWorker-271:\n",
      "Process ForkPoolWorker-198:\n",
      "Process ForkPoolWorker-199:\n",
      "Process ForkPoolWorker-282:\n",
      "Process ForkPoolWorker-221:\n",
      "Process ForkPoolWorker-230:\n",
      "Process ForkPoolWorker-225:\n",
      "Process ForkPoolWorker-270:\n",
      "Process ForkPoolWorker-302:\n",
      "Process ForkPoolWorker-216:\n",
      "Process ForkPoolWorker-290:\n",
      "Process ForkPoolWorker-272:\n",
      "Process ForkPoolWorker-261:\n",
      "Process ForkPoolWorker-255:\n",
      "Process ForkPoolWorker-260:\n",
      "Process ForkPoolWorker-248:\n",
      "Process ForkPoolWorker-209:\n",
      "Process ForkPoolWorker-289:\n",
      "Process ForkPoolWorker-309:\n",
      "Process ForkPoolWorker-243:\n",
      "Process ForkPoolWorker-233:\n",
      "Process ForkPoolWorker-205:\n",
      "Process ForkPoolWorker-244:\n",
      "Process ForkPoolWorker-228:\n",
      "Process ForkPoolWorker-274:\n",
      "Process ForkPoolWorker-224:\n",
      "Process ForkPoolWorker-208:\n",
      "Process ForkPoolWorker-268:\n",
      "Process ForkPoolWorker-256:\n",
      "Process ForkPoolWorker-294:\n",
      "Process ForkPoolWorker-197:\n",
      "Process ForkPoolWorker-263:\n",
      "Process ForkPoolWorker-293:\n",
      "Process ForkPoolWorker-200:\n",
      "Process ForkPoolWorker-281:\n",
      "Process ForkPoolWorker-222:\n",
      "Process ForkPoolWorker-291:\n",
      "Process ForkPoolWorker-262:\n",
      "Process ForkPoolWorker-295:\n",
      "Process ForkPoolWorker-259:\n",
      "Process ForkPoolWorker-238:\n",
      "Process ForkPoolWorker-267:\n",
      "Process ForkPoolWorker-279:\n",
      "Process ForkPoolWorker-253:\n",
      "Process ForkPoolWorker-311:\n",
      "Process ForkPoolWorker-258:\n",
      "Process ForkPoolWorker-292:\n",
      "Process ForkPoolWorker-280:\n",
      "Process ForkPoolWorker-287:\n",
      "Process ForkPoolWorker-288:\n",
      "Process ForkPoolWorker-316:\n",
      "Process ForkPoolWorker-275:\n",
      "Process ForkPoolWorker-194:\n",
      "Process ForkPoolWorker-285:\n",
      "Process ForkPoolWorker-283:\n",
      "Process ForkPoolWorker-315:\n",
      "Process ForkPoolWorker-273:\n",
      "Process ForkPoolWorker-265:\n",
      "Process ForkPoolWorker-296:\n",
      "Process ForkPoolWorker-223:\n",
      "Process ForkPoolWorker-193:\n",
      "Process ForkPoolWorker-269:\n",
      "Process ForkPoolWorker-314:\n",
      "Traceback (most recent call last):\n",
      "Process ForkPoolWorker-286:\n",
      "Process ForkPoolWorker-312:\n",
      "Process ForkPoolWorker-232:\n",
      "Process ForkPoolWorker-203:\n",
      "Process ForkPoolWorker-297:\n",
      "Process ForkPoolWorker-264:\n",
      "Process ForkPoolWorker-212:\n",
      "Process ForkPoolWorker-201:\n",
      "Process ForkPoolWorker-308:\n",
      "Process ForkPoolWorker-299:\n",
      "Process ForkPoolWorker-219:\n",
      "Process ForkPoolWorker-284:\n",
      "Process ForkPoolWorker-207:\n",
      "Process ForkPoolWorker-257:\n",
      "Process ForkPoolWorker-313:\n",
      "Process ForkPoolWorker-196:\n",
      "Process ForkPoolWorker-277:\n",
      "Process ForkPoolWorker-300:\n",
      "Process ForkPoolWorker-217:\n",
      "Process ForkPoolWorker-301:\n",
      "Process ForkPoolWorker-206:\n",
      "Process ForkPoolWorker-229:\n",
      "Process ForkPoolWorker-304:\n",
      "Process ForkPoolWorker-246:\n",
      "Process ForkPoolWorker-247:\n",
      "Traceback (most recent call last):\n",
      "Traceback (most recent call last):\n",
      "Traceback (most recent call last):\n",
      "Traceback (most recent call last):\n"
     ]
    }
   ],
   "source": [
    "from spn.algorithms.LearningWrappers import learn_parametric, learn_classifier\n",
    "from spn.structure.leaves.parametric.Parametric import Categorical, Gaussian, Geometric\n",
    "from spn.structure.StatisticalTypes import MetaType\n",
    "from spn.structure.Base import Context\n",
    "from spn.algorithms.MPE import mpe"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c4541fb4-4084-4df8-adfa-76b5062245e3",
   "metadata": {},
   "source": [
    "#### learn_parametric "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "id": "2bc1a3bb-e1be-4a9a-8664-109b60f81de0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<spn.structure.Base.Context at 0x7f84799cfa60>"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "context_list = [MetaType.REAL]*13\n",
    "context_list = context_list + [MetaType.DISCRETE, MetaType.DISCRETE]\n",
    "# print(context_list)\n",
    "ds_context = Context(meta_types=context_list)\n",
    "ds_context.add_domains(train_items)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "84c2e672-c1a7-4809-94d3-3bdde2aa1250",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "ename": "AssertionError",
     "evalue": "for parametric leaves, the ds_context.parametric_types can't be None",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAssertionError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-23-f5f2d2abb66c>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mspn\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mlearn_parametric\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtrain_items\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mds_context\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmin_instances_slice\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m20\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m~/software/anaconda3/envs/PY38/lib/python3.8/site-packages/spn/algorithms/LearningWrappers.py\u001b[0m in \u001b[0;36mlearn_parametric\u001b[0;34m(data, ds_context, cols, rows, min_instances_slice, min_features_slice, multivariate_leaf, cluster_univariate, threshold, ohe, leaves, memory, rand_gen, cpus)\u001b[0m\n\u001b[1;32m    171\u001b[0m         \u001b[0mlearn_param\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmemory\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcache\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlearn_param\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    172\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 173\u001b[0;31m     \u001b[0;32mreturn\u001b[0m \u001b[0mlearn_param\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mds_context\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcols\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrows\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmin_instances_slice\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mthreshold\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mohe\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    174\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    175\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/software/anaconda3/envs/PY38/lib/python3.8/site-packages/spn/algorithms/LearningWrappers.py\u001b[0m in \u001b[0;36mlearn_param\u001b[0;34m(data, ds_context, cols, rows, min_instances_slice, threshold, ohe)\u001b[0m\n\u001b[1;32m    166\u001b[0m         \u001b[0mnextop\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mget_next_operation\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmin_instances_slice\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmin_features_slice\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmultivariate_leaf\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcluster_univariate\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    167\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 168\u001b[0;31m         \u001b[0;32mreturn\u001b[0m \u001b[0mlearn_structure\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mds_context\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msplit_rows\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msplit_cols\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mleaves\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnextop\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    169\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    170\u001b[0m     \u001b[0;32mif\u001b[0m \u001b[0mmemory\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/software/anaconda3/envs/PY38/lib/python3.8/site-packages/spn/algorithms/StructureLearning.py\u001b[0m in \u001b[0;36mlearn_structure\u001b[0;34m(dataset, ds_context, split_rows, split_cols, create_leaf, next_operation, initial_scope, data_slicer)\u001b[0m\n\u001b[1;32m    292\u001b[0m         \u001b[0;32melif\u001b[0m \u001b[0moperation\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0mOperation\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mCREATE_LEAF\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    293\u001b[0m             \u001b[0mleaf_start_t\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mperf_counter\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 294\u001b[0;31m             \u001b[0mnode\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcreate_leaf\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlocal_data\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mds_context\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mscope\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    295\u001b[0m             \u001b[0mparent\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mchildren\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mchildren_pos\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnode\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    296\u001b[0m             \u001b[0mleaf_end_t\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mperf_counter\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/software/anaconda3/envs/PY38/lib/python3.8/site-packages/spn/structure/leaves/parametric/Parametric.py\u001b[0m in \u001b[0;36mcreate_parametric_leaf\u001b[0;34m(data, ds_context, scope)\u001b[0m\n\u001b[1;32m    345\u001b[0m     \u001b[0midx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mscope\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    346\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 347\u001b[0;31m     assert (\n\u001b[0m\u001b[1;32m    348\u001b[0m         \u001b[0mds_context\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mparametric_types\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    349\u001b[0m     ), \"for parametric leaves, the ds_context.parametric_types can't be None\"\n",
      "\u001b[0;31mAssertionError\u001b[0m: for parametric leaves, the ds_context.parametric_types can't be None"
     ]
    }
   ],
   "source": [
    "spn = learn_parametric(train_items, ds_context, min_instances_slice=20)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a5e704e1-1082-47e4-bea4-7f4c7aecf050",
   "metadata": {},
   "source": [
    "#### learn_classifier"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "id": "3c4abaca-21c9-4e10-a619-d302f13ae0a7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<spn.structure.Base.Context at 0x7f842e828c10>"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "context_list = [Gaussian]*13\n",
    "context_list = context_list + [ Gaussian, Categorical]\n",
    "# print(context_list)\n",
    "ds_context = Context(parametric_types=context_list)\n",
    "ds_context.add_domains(train_items)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "id": "f3ccce83-9a5e-4c15-b439-f709abfad7b9",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "spn_classification = learn_classifier(train_items,\n",
    "                       ds_context,\n",
    "                       learn_parametric, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "id": "f096ded7-64ae-4764-af75-9cce052054d3",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/zhangshen/software/anaconda3/envs/PY38/lib/python3.8/site-packages/spn/structure/leaves/parametric/Inference.py:88: RuntimeWarning: divide by zero encountered in log\n",
      "  probs[idx_in] = np.array(np.log(node.p))[cat_data[~out_domain_ids]]\n"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-109-5de8079d99c2>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmpe\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mspn_classification\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtest_x\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m~/software/anaconda3/envs/PY38/lib/python3.8/site-packages/spn/algorithms/MPE.py\u001b[0m in \u001b[0;36mmpe\u001b[0;34m(node, input_data, node_top_down_mpe, node_bottom_up_mpe_log, in_place)\u001b[0m\n\u001b[1;32m    101\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    102\u001b[0m     \u001b[0;31m# one pass bottom up evaluating the likelihoods\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 103\u001b[0;31m     \u001b[0mlog_likelihood\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnode\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdata\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mdata\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdtype\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnode_log_likelihood\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mnode_bottom_up_mpe_log\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlls_matrix\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mlls_per_node\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    104\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    105\u001b[0m     \u001b[0minstance_ids\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/software/anaconda3/envs/PY38/lib/python3.8/site-packages/spn/algorithms/Inference.py\u001b[0m in \u001b[0;36mlog_likelihood\u001b[0;34m(node, data, dtype, node_log_likelihood, lls_matrix, debug, **kwargs)\u001b[0m\n\u001b[1;32m    130\u001b[0m def log_likelihood(\n\u001b[1;32m    131\u001b[0m         node, data, dtype=np.float64, node_log_likelihood=_node_log_likelihood, lls_matrix=None, debug=False, **kwargs):\n\u001b[0;32m--> 132\u001b[0;31m     return likelihood(node, data, dtype=dtype, node_likelihood=node_log_likelihood, lls_matrix=lls_matrix, debug=debug,\n\u001b[0m\u001b[1;32m    133\u001b[0m                       **kwargs)\n\u001b[1;32m    134\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/software/anaconda3/envs/PY38/lib/python3.8/site-packages/spn/algorithms/Inference.py\u001b[0m in \u001b[0;36mlikelihood\u001b[0;34m(node, data, dtype, node_likelihood, lls_matrix, debug, **kwargs)\u001b[0m\n\u001b[1;32m    118\u001b[0m         \u001b[0mnode_likelihood\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m{\u001b[0m\u001b[0mk\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mexec_funct\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mk\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mnode_likelihood\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mkeys\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    119\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 120\u001b[0;31m     result = eval_spn_bottom_up(node, node_likelihood, all_results=all_results, debug=debug, dtype=dtype, data=data,\n\u001b[0m\u001b[1;32m    121\u001b[0m                                 **kwargs)\n\u001b[1;32m    122\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/software/anaconda3/envs/PY38/lib/python3.8/site-packages/spn/structure/Base.py\u001b[0m in \u001b[0;36meval_spn_bottom_up\u001b[0;34m(node, eval_functions, all_results, debug, **args)\u001b[0m\n\u001b[1;32m    393\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    394\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0mn_is_leaf\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 395\u001b[0;31m             \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mfunc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mn\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    396\u001b[0m         \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    397\u001b[0m             \u001b[0mlen_children\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mn\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mchildren\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/software/anaconda3/envs/PY38/lib/python3.8/site-packages/spn/structure/leaves/parametric/MPE.py\u001b[0m in \u001b[0;36mparam_bu_fn\u001b[0;34m(node, data, dtype)\u001b[0m\n\u001b[1;32m     29\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mget_parametric_bottom_up_log_ll\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mll_func\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmode_func\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     30\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0mparam_bu_fn\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnode\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdata\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfloat64\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 31\u001b[0;31m         \u001b[0mprobs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mll_func\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnode\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdata\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mdata\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mdtype\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     32\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     33\u001b[0m         \u001b[0mmpe_ids\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0misnan\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnode\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mscope\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/software/anaconda3/envs/PY38/lib/python3.8/site-packages/spn/structure/leaves/parametric/Inference.py\u001b[0m in \u001b[0;36mcontinuous_log_likelihood\u001b[0;34m(node, data, dtype, **kwargs)\u001b[0m\n\u001b[1;32m     21\u001b[0m     \u001b[0mprobs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmarg_ids\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mobservations\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mleaf_marginalized_likelihood\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnode\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdata\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlog_space\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     22\u001b[0m     \u001b[0mscipy_obj\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mparams\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mget_scipy_obj_params\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnode\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 23\u001b[0;31m     \u001b[0mprobs\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m~\u001b[0m\u001b[0mmarg_ids\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mscipy_obj\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlogpdf\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mobservations\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mparams\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     24\u001b[0m     \u001b[0;32mreturn\u001b[0m \u001b[0mprobs\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     25\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/software/anaconda3/envs/PY38/lib/python3.8/site-packages/scipy/stats/_distn_infrastructure.py\u001b[0m in \u001b[0;36mlogpdf\u001b[0;34m(self, x, *args, **kwds)\u001b[0m\n\u001b[1;32m   1910\u001b[0m         \u001b[0mdtyp\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfind_common_type\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdtype\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfloat64\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1911\u001b[0m         \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0masarray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0mloc\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0mscale\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mdtyp\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1912\u001b[0;31m         \u001b[0mcond0\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_argcheck\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m&\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mscale\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   1913\u001b[0m         \u001b[0mcond1\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_support_mask\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m&\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mscale\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1914\u001b[0m         \u001b[0mcond\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcond0\u001b[0m \u001b[0;34m&\u001b[0m \u001b[0mcond1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "result = mpe(spn_classification, test_x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f5831d06-123b-40a8-b707-f51580df842c",
   "metadata": {},
   "outputs": [],
   "source": [
    "y_pred = result[:,-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cc5d6bfb-276a-49c8-b340-efa55f337b84",
   "metadata": {},
   "outputs": [],
   "source": [
    "from spn.io.Graphics import plot_spn\n",
    "plot_spn(spn_classification)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "960c0083-e1ab-4480-a4b7-0a608f3cf5df",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "c83088fa-5936-4386-997c-ad1bf05dce91",
   "metadata": {},
   "source": [
    "### SKlearn训练模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "225efd08-d94f-4d66-9f35-c84fcd50ae8f",
   "metadata": {},
   "outputs": [],
   "source": [
    "#k近邻函数\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "from sklearn.neural_network import MLPClassifier\n",
    "from sklearn.metrics import accuracy_score\n",
    "from sklearn.metrics import precision_score\n",
    "from sklearn.metrics import recall_score\n",
    "from sklearn.metrics import f1_score\n",
    "import pickle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "48aa21de-ab1c-455a-bd55-a4d063f5e3d5",
   "metadata": {},
   "outputs": [],
   "source": [
    "train_x = train_items[:,:-1]\n",
    "train_y = train_items[:,-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "90b9cef1-ad26-4cd9-b6f8-9caedb568cc7",
   "metadata": {},
   "outputs": [],
   "source": [
    "test_x = test_items[:,:-1]\n",
    "test_y = test_items[:,-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "384b3753-68ab-4e24-963c-ff78a7e57892",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1340484, 14)\n",
      "(574362, 14)\n",
      "(1340484,)\n",
      "(574362,)\n"
     ]
    }
   ],
   "source": [
    "print(train_x.shape)\n",
    "print(test_x.shape)\n",
    "print(train_y.shape)\n",
    "print(test_y.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e2064173-48ce-4b7e-a419-a69164d9eb39",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "ff08492e-7480-4615-8a1c-0cbfb5afb045",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "KNeighborsClassifier()"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#设置knn分类器\n",
    "knn = KNeighborsClassifier()\n",
    "#进行训练\n",
    "knn.fit(train_x,train_y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8b02434b-7bbc-4d5d-9d68-aa4a8c14b1c2",
   "metadata": {},
   "outputs": [],
   "source": [
    "#使用训练好的knn进行数据预测\n",
    "y_pred = knn.predict(test_x)\n",
    "# print(y_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "55c30f03-19d9-42a6-997e-4729f0a15d20",
   "metadata": {},
   "outputs": [],
   "source": [
    "clf = MLPClassifier(solver='lbfgs')\n",
    "clf.fit(train_x,train_y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "id": "33c92326-9b9f-4133-81ae-6815a9f53c61",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "MLPClassifier(solver='lbfgs')"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "447fc6c1-895c-4ca6-ac9d-d573979957bd",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 保存模型\n",
    "with open('models/clf.pickle', 'wb') as f:\n",
    "    pickle.dump(clf, f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eec1729c-8ced-45a8-abd8-6f8bc0b90da2",
   "metadata": {},
   "outputs": [],
   "source": [
    "#读取Model\n",
    "with open('models/clf.pickle', 'rb') as f:\n",
    "    clf = pickle.load(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "bb58b064-b32c-4ce5-9947-22dc56ad5927",
   "metadata": {},
   "outputs": [],
   "source": [
    "def predict_model(test):\n",
    "    test = test.reshape(1,-1)\n",
    "    return clf.predict(test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "478a21ba-0a05-48f6-bf59-d1c214c1f04f",
   "metadata": {},
   "outputs": [],
   "source": [
    "arg_list = list(test_x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "34833e6b-d357-4baf-b8e8-a839bb9df247",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-288.62063599,    7.0677762 ,   48.53384781,    7.41235447,\n",
       "        -22.5753479 ,  -10.47899818,   36.48366165,    6.38537502,\n",
       "          2.80917382,   14.51070976,   13.29878044,    7.13409901,\n",
       "        -17.4593811 ,    0.        ])"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_x[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "44b4f6c5-9166-4a40-96b9-41573fc345bc",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 574362/574362 [00:53<00:00, 10717.98it/s]\n"
     ]
    }
   ],
   "source": [
    "pool = multiprocessing.Pool(processes=16)\n",
    "with pool as workers:\n",
    "    data_container = list(tqdm(workers.imap(predict_model,arg_list),total=len(arg_list)))\n",
    "workers.close()\n",
    "workers.join()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "e254f2dd-a552-4c36-b3d2-409dcbc7ba59",
   "metadata": {},
   "outputs": [],
   "source": [
    "y_pred = np.array(data_container)\n",
    "y_pred = y_pred.T[0]\n",
    "y_test = test_y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "id": "3549c48c-ec0e-4db1-80ff-919f28c9e0f3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[4. 9. 4. 4. 2. 9. 9. 9. 9. 9.]\n",
      "[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n"
     ]
    }
   ],
   "source": [
    "print(y_pred[0:10])\n",
    "print(y_test[0:10])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "9d7580aa-e67a-40a3-998b-1cab56df2de3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.1805864594106156\n",
      "0.18101640362674112\n",
      "0.1805864594106156\n",
      "0.17883857825066146\n"
     ]
    }
   ],
   "source": [
    "s_accuracy = accuracy_score(y_test, y_pred)\n",
    "s_precision = precision_score(y_test, y_pred, average='weighted')\n",
    "s_recall = recall_score(y_test, y_pred, average='weighted')\n",
    "s_f1 = f1_score(y_test, y_pred, average='weighted')\n",
    "print(s_accuracy)\n",
    "print(s_precision)\n",
    "print(s_recall)\n",
    "print(s_f1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "94208363-09e6-42bd-a843-25ff00cb4735",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
